﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Titan.FIS.Barcode.TwoBarcode;
using Titan.FIS.Enum;

namespace Titan.FIS.SampleRecd
{
    public partial class frmView : Form
    {
        /// <summary>
        /// 编码（会打印出来），试样编码或者化验编码
        /// </summary>
        private readonly string _strCode;
        /// <summary>
        /// 试样编码更新打印次数用
        /// </summary>
        private readonly string _strSCode;
        /// <summary>
        /// 编码类型（会打印出来）
        /// </summary>
        private readonly string _strType;
        /// <summary>
        /// 打印类型,0：试样编码，1:化验编码 2：批次编码 3：根据试样编码更新化验编码
        /// </summary>
        private readonly string _strPrintType;

        /// <summary>
        /// 是否打印标签数字
        /// </summary>
        private readonly int _iPrintTestCodeNum;

        /// <summary>
        /// 打印描述
        /// </summary>
        private readonly string _strDesc;

        /// <summary>
        /// 打印条码类型
        /// </summary>
        private readonly int _iPrintBarcodeType;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public frmView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="strCode">编码（会打印出来），试样编码或者化验编码</param>
        /// <param name="strSCode">匹配编码，用于更新打印次数</param>
        /// <param name="strType">编码类型（会打印出来）</param>
        /// <param name="strPrintType">打印类型,0：试样编码，1:化验编码 2：批次编码 3：根据试样编码更新化验编码</param>
        /// <param name="strDesc">描述</param>
        /// <param name="iPrintBarcodeType">打印条码类型</param>
        public frmView(string strCode, string strSCode, string strType, string strPrintType, int iPrintTestCodeNum, string strDesc, int iPrintBarcodeType)
        {
            InitializeComponent();
            _strCode = strCode;
            _strSCode = strSCode;
            _strType = strType;
            _strPrintType = strPrintType;
            _iPrintTestCodeNum = iPrintTestCodeNum;
            _strDesc = strDesc;
            _iPrintBarcodeType = iPrintBarcodeType;
        }

        /// <summary>
        /// 界面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmView_Load(object sender, EventArgs e)
        {
            Image img;
            if (_iPrintBarcodeType == 1)
            {
                var gen = new TagGen();
                img = gen.GetImgASample(_strType, _strCode, _iPrintTestCodeNum);
            }
            else
            {
                var strDesc = _strDesc.Length <= 2 && !string.IsNullOrEmpty(_strDesc) ? _strDesc + "号" : _strDesc;
                var strDescSize = strDesc.Length <= 2 ? "60" : (strDesc.Length * 2 * 10).ToString();
                img = Barcode.TwoDimensionBarcode.Encode(QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeEncoder.ERROR_CORRECTION.M, "4", "7", _strCode, "", strDesc, strDescSize);
            }
            int height = 60;
            int font = 10;
            var content = "";
            switch (_strPrintType)
            {
                case "0":
                    content = _strType;
                    height = 60;
                    font = 19;
                    break;
                case "1":
                case "3":
                    content = _strType; //"123456789123456789123456Mt,Qbad,Vad,Aad,Mad,Stad";
                    height = 80;
                    font = 10;
                    break;
                case "2":
                    content = "批次编码";
                    height = 60;
                    font = 15;
                    break;
                default:
                    break;
            }
            img = Barcode.TwoDimensionBarcode.CombinImageFloor(img, content, font, height);

            //var img = (System.Drawing.Bitmap)img;
            picView.Image = img;
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            var obj = new TagPrint(picView.Image, _iPrintBarcodeType);
            if (!obj.Print())
            {
                MessageBox.Show(string.Format("打印标签号{0}失败！", _strCode));
            }
            else
            {
                var strErrInfo = string.Empty;
                var bSaveResult = DataOperate.DataExecProc.DoUpdatePrintCount(_strSCode, _strPrintType, ref strErrInfo);
                if (!bSaveResult)
                {
                    MessageBox.Show(string.Format("更新标签{0}打印次数失败！原因：{1}", _strCode, strErrInfo));
                }
                else
                    MessageBox.Show(string.Format("打印编码{0}完毕，谢谢！", _strCode));
            }
        }

    }
}
